import { get, post, getCity } from '../../utils/request.js'
import global from '../../utils/global.js'
let app = getApp();
let that
Page({
  data: {
    currentDate: "2018年10月03日",
    dayList: '',
    currentDayList: '',
    currentObj: '',
    currentDay: '',
    arrNO:[],
    //日期初始化选中样式
    selectCSS: 'bk-color-day',
  },
  onLoad: function (options) {
    that = this;
    console.log(options);
    let currentObj = that.getCurrentDayString()
    that.setData({
      currentDate: currentObj.getFullYear() + '/' + (currentObj.getMonth() + 1) + '/' + currentObj.getDate(),
      currentDay: currentObj.getDate(),
      currentObj: currentObj,
      /*  获取当前的年、月  */
      currentYear: currentObj.getFullYear(),
      currentMonth: (currentObj.getMonth() + 1),
      productId: options.productId*1
    })
    that.setSchedule(currentObj);
    that.isCanAppointmentList(that.data.currentYear, that.data.currentMonth)
    // that.isDay()
  },
  doDay: function (e) {
    that.setData({
      dataInfo: null
    })
    let currentObj = that.data.currentObj
    let Y = currentObj.getFullYear();
    let m = currentObj.getMonth() + 1;
    let d = currentObj.getDate();
    let str = ''
    if (e.currentTarget.dataset.key == 'left') {
      m -= 1
      if (m <= 0) {
        str = (Y - 1) + '/' + 12 + '/' + d
      } else {
        str = Y + '/' + m + '/' + d
      }
    } else {
      m += 1
      if (m <= 12) {
        str = Y + '/' + m + '/' + d
      } else {
        str = (Y + 1) + '/' + 1 + '/' + d
      }
    }
    currentObj = new Date(str)
    that.setData({
      currentDate: currentObj.getFullYear() + '/' + (currentObj.getMonth() + 1) + '/' + currentObj.getDate(),
      currentObj: currentObj,
      /*  获取当前的年、月  */
      currentYear: currentObj.getFullYear(),
      currentMonth: (currentObj.getMonth() + 1),
     
    })
    console.log("选择当前年：" + that.data.currentYear);
    console.log("选择当前月：" + that.data.currentMonth);

    that.isCanAppointmentList(that.data.currentYear, that.data.currentMonth)
    that.setSchedule(currentObj);
    // that.isDay()
  },
  getCurrentDayString: function () {
    let objDate = this.data.currentObj
    if (objDate != '') {
      return objDate
    } else {
      let c_obj = new Date()
      let a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
      return new Date(a)
    }
  },
  setSchedule: function (currentObj) {
    let m = currentObj.getMonth() + 1
    let Y = currentObj.getFullYear()
    let d = currentObj.getDate();
    let dayString = Y + '/' + m + '/' + currentObj.getDate()
    let currentDayNum = new Date(Y, m, 0).getDate()
    let currentDayWeek = currentObj.getUTCDay() + 1
    let result = currentDayWeek - (d % 7 - 1);
    let firstKey = result <= 0 ? 7 + result : result;
    let currentDayList = [];
    let f = 0
    for (let i = 0; i < 42; i++) {
      let data = []
      if (i < firstKey - 1) {
        currentDayList[i] = ''
      } else {
        if (f < currentDayNum) {
          currentDayList[i] = f + 1;
          f = currentDayList[i]
        } else if (f >= currentDayNum) {
          currentDayList[i] = ''
        }
      }
    }
    // console.log(currentDayList,"jjaj")
    that.setData({
      currentDayList: currentDayList
    })
  },
  
  //选择具体日期方法--xzz1211
  selectDay: function (e) {
    let clickDay = e.currentTarget.dataset.day;
    if (that.isInArray(that.data.arrNew, clickDay)){
      wx.showToast({
        title: '不可预约',
      })
      return
    }
    that.setData({
      currentDay: e.currentTarget.dataset.day,//选择的数据，非真实当前日期
      currentDa: e.currentTarget.dataset.day, //选择某月具体的一天
      currentDate: that.data.currentYear + '-' + that.data.currentMonth + '-' + e.currentTarget.dataset.day,//真实选择数据
    })
    console.log("当前选择日期：" + that.data.currentDate);
    wx.setStorageSync("selectCurrentDate", that.data.currentDate);
    wx.showToast({
      title: '已选择',
      icon: 'succes',
      duration: 1000,
      mask: true
    })

    let pages = getCurrentPages(); 
    let prevPage = pages[pages.length - 2]; 
    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
      prev:true,
    })
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 1000)
  },
   // 可约时间IsCanAppointmentList
  isCanAppointmentList(year, month) {
    let param ={
      productId: that.data.productId,
      year: year,
      month: month
    }
    post(global.apiUrl.IsCanAppointmentList, param, (res) => {
      let arr1 = [];
      if (res.obj){
        res.obj.forEach((item, index) => {
          if (item.formatStatus == "不可约") {
            arr1.push(item.day*1)
          }
        })    
      }
      let arr2 = that.data.currentDayList;
      let arrNorep = that.getArrDifference(arr1, arr2)
      that.setData({
        arrNew: arr1,
        arrNorep: arrNorep,
        dataInfo: res.obj
      })
      
    })
  },
  getArrDifference(arr1, arr2) {
    return arr1.concat(arr2).filter(function (v, i, arr) {
      return arr.indexOf(v) === arr.lastIndexOf(v);
    });
  },

  // 匹配日期
  isInArray(arr, value) {
    for (var i = 0; i < arr.length; i++) {
      if (value === arr[i]) {
        return true;
      }
    }
    return false;
  }
})